//
//  StudyHomeTableVC.m
//  Cunli
//
//  Created by lilubin on 15/7/7.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "StudyHomeTableVC.h"
#import "NSString+MD5.h"
#import "MyDataService.h"
#import "NewsModel_yy.h"
#import "UIViewExt.h"
#import "SwipableViewController.h"
#import "Config.h"
#import "LearnModel.h"
#import "LearnCellHome.h"
#import "NewsCellStudy.h"
#import "BookDetailVC.h"
#import "MediaDetailVC.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define pageSize 6

static NSString *kLearnCellHomeID = @"LearnCellHome";
static NSString *kNewsCellStudyID = @"NewsCellStudy";


@interface StudyHomeTableVC ()
{
    NSMutableArray *_allDataModelArray; //法律法规，普法教材，普法讲堂三个部分所有model数据
    NSMutableArray *_flfgDataModelArray; //法律法规model数据
    NSMutableArray *_pfjcDataModelArray; //普法教材model数据
    NSMutableArray *_pfjtDataModelArray; //普法讲堂model数据
    NSArray *_dailyTitles; 
}

@end

@implementation StudyHomeTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.rowHeight = 90;
    [self initAllAttributes];
    [self loadNewsData];
    [self registerNewsCell];
    self.tableView.separatorColor = [UIColor colorWithHexString:TableViewSeperateColorValue];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initAllAttributes
{
    self.tableView.bounces = NO;
    _flfgDataModelArray = [NSMutableArray arrayWithCapacity:3];
    _pfjcDataModelArray = [NSMutableArray arrayWithCapacity:3];
    _pfjtDataModelArray = [NSMutableArray arrayWithCapacity:3];
    _allDataModelArray = [NSMutableArray arrayWithCapacity:9];
    
    _dailyTitles = [NSArray arrayWithObjects:@"法律法规", @"普法教材", @"普法讲堂", nil];
}

- (instancetype)init
{
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
        
    }
    return self;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _allDataModelArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = [NSArray arrayWithArray:_allDataModelArray[section]];
    return array.count;
}

/*
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
 */

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        NewsCellStudy *cell = [tableView dequeueReusableCellWithIdentifier:kNewsCellStudyID forIndexPath:indexPath];
        cell.newsModel = _allDataModelArray[indexPath.section][indexPath.row];
        return cell;
    }
//  LearnCellHome *cell = [tableView dequeueReusableCellWithIdentifier:kLearnCellHomeID forIndexPath:indexPath];
    LearnCellHome *cell = [[NSBundle mainBundle] loadNibNamed:@"LearnCellHome" owner:self options:nil].lastObject;
    cell.learnModel = _allDataModelArray[indexPath.section][indexPath.row];
   
    //    UIImageView *videoMarkView = [[UIImageView alloc] initWithFrame:CGRectMake(33, 23, 44, 44)];
    CGFloat itemWidth = 40;
    UIImageView *videoMarkView = [[UIImageView alloc] initWithFrame:CGRectMake((cell.titleImgView.frame.size.width*1.0-itemWidth)/2.0+8, (cell.titleImgView.frame.size.height*1.0-itemWidth)/2.0+15, itemWidth, itemWidth)];
    [videoMarkView setImage:[UIImage imageNamed:@"video_icon"]];
    
    if ([cell.learnModel.type isEqualToString:@"1"]) {
        [cell.contentView addSubview:videoMarkView];
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        BookDetailVC *bookVC = [[BookDetailVC alloc] init];
        NewsModel_yy *model = _allDataModelArray[indexPath.section][indexPath.row];
        bookVC.bookID = model.itemid;
        bookVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:bookVC animated:YES];
    }
    else if(indexPath.section == 1){
        LearnModel *selModel = _allDataModelArray[indexPath.section][indexPath.row];
        if ([selModel.type isEqualToString:@"0"])
        {
            BookDetailVC *detailVC = [[BookDetailVC alloc] init];
            detailVC.bookID = selModel.itemid;
            detailVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:detailVC animated:YES];
        }
        else if([selModel.type isEqualToString:@"1"] || [selModel.type isEqualToString:@"2"])
        {
            MediaDetailVC *detailVC = [[MediaDetailVC alloc] init];
            detailVC.mediaID = selModel.itemid;
            detailVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:detailVC animated:YES];
        }

    }
    else
    {
        MediaDetailVC *mediaVC = [[MediaDetailVC alloc] init];
        LearnModel *model = _allDataModelArray[indexPath.section][indexPath.row];
        mediaVC.mediaID = model.itemid;
        mediaVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:mediaVC animated:YES];
    }
}


- (void)registerNewsCell
{
    UINib *nib = [UINib nibWithNibName:kNewsCellStudyID bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:kNewsCellStudyID];
    
    UINib *nib2 = [UINib nibWithNibName:kLearnCellHomeID bundle:nil];
    [self.tableView registerNib:nib2 forCellReuseIdentifier:kLearnCellHomeID];
}

- (void)loadNewsData
{
    NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:5];
    
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    [MyDataService requestURL:@"study.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
    
        NSString *response = [result objectForKey:@"response"];
        if ([response isEqualToString:@"study"])
        {
            NSDictionary *newslistDic = result[@"studylist"];
            NSLog(@"学习推荐页:%@", newslistDic);
            NSArray *flfgArray = newslistDic[@"flfg_sylist"]; //法律法规
            NSArray *pfjcArray = newslistDic[@"pfjc_sylist"]; //普法教材
            NSArray *pfjtArray = newslistDic[@"pfjt_sylist"]; //普法讲堂
            
            for(NSDictionary *flfgdataDic in flfgArray)
            {
                NewsModel_yy *newModel = [NewsModel_yy yy_modelWithJSON:flfgdataDic];
                [_flfgDataModelArray addObject:newModel];
            }
            
            for(NSDictionary *pfjcdataDic in pfjcArray)
            {
//                LearnModel *newModel = [[LearnModel alloc] initWithDataDic:pfjcdataDic];
                LearnModel *newModel = [LearnModel yy_modelWithJSON:pfjcdataDic];
                newModel.modelType = LearnModelTypeAuthor;
                [_pfjcDataModelArray addObject:newModel];
            }
            
            for(NSDictionary *pfjtdataDic in pfjtArray)
            {
//                LearnModel *newModel = [[LearnModel alloc] initWithDataDic:pfjtdataDic];
                LearnModel *newModel = [LearnModel yy_modelWithJSON:pfjtdataDic];
                newModel.modelType = LearnModelTypeSpeaker;
                [_pfjtDataModelArray addObject:newModel];
                
            }
            
            _allDataModelArray = [NSMutableArray arrayWithObjects:_flfgDataModelArray, _pfjcDataModelArray, _pfjtDataModelArray, nil];
        }
        
        if ([response isEqualToString:@"error"]) {
            NSDictionary *errorDic = [result objectForKey:@"error"];
            NSString *text = errorDic[@"text"];
            [Config showHUDWithText:text andImage:@"error"];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
        });
        
    } failureAction:^{
        [Config showHUDWithText:@"网络错误" andImage:@"error"];
    }];
}


@end
